package com.xmhmyh.generalframework.common.wxutils.Handlers;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xmhmyh.generalframework.common.wxutils.entity.WxJsApiResult;
import com.xmhmyh.generalframework.common.wxutils.entity.WxLoginResult;
import com.xmhmyh.generalframework.common.wxutils.entity.WxTokenResult;
import com.xmhmyh.generalframework.common.wxutils.entity.WxUserInfoResult;
import com.xmhmyh.generalframework.common.wxutils.util.HttpUtil;
import com.xmhmyh.generalframework.common.wxutils.util.WxUrl;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 于丁
 * 微信二维码相关处理
 * @DateTime: 2020/4/20 1:59 下午
 */
public class WxQrcodeHandler extends WxHandler {


    /**
     * 生成总Token
     *
     * @return
     */
    public WxTokenResult getWxSDKAccessToken(String appId, String appSecret) {
        try {
            String result = HttpUtil.get(WxUrl.getWxAccessToken(appId, appSecret));
            System.out.println("获取WxSdkaccessToken:" + result);
            WxTokenResult wxTokenResult = JSONObject.parseObject(result, WxTokenResult.class);
            return wxTokenResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成微信有限制二维码
     * i
     *
     * @param path
     * @return
     * @throws Exception
     */
    public byte[] createWxQRCode(String path, String appId, String appSecret) {
        WxTokenResult wxSDKAccessToken = getWxSDKAccessToken(appId, appSecret);
        if (wxSDKAccessToken.getErrmsg() != null) {
            System.out.println("获取WxSdkaccessToken失败");
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("path", path);
        byte[] bytes = HttpUtil.postJson5Stream(WxUrl.getQrCodeUrl(wxSDKAccessToken.getAccess_token()), JSON.toJSONString(params));
        return bytes;
    }

}
